
/**
 * 资源加载模块
 */
var res_mgr = cc.Class({
    extends: cc.Component,

    statics: {
        Instance: null,
    },

    properties: { 
        //这里是加载启动开始需要依赖的资源
        lancher_prefabs: {
            type: cc.Prefab,
            default: [],
        },

        lancher_sprite_frames: {
            type: cc.SpriteFrame,
            default: [],
        },

        lancher_audio_clips: {
            type: cc.AudioClip,
            default: [],
        },

        lancher_sprite_atlases: {
            type: cc.SpriteAtlas,
            default: [],
        },
    },

    onLoad() {
        if (res_mgr.Instance === null) {
            res_mgr.Instance = this;
        } else {
            this.destroy();
            cc.error("[error:] res_mgr has multi instances!!!");
            return;
        }
    },

    /**
     * 预加载资源包
     * res_set 资源合集 ： {prefabs:[], sprite_frame:[], audio:[], sprite_atals:[]}
     * on_progress: 进度条函数： function(per) [0,1]
     * on_load_finished: 加载结束： function(){}
     */
    preload_res_pkg(res_set, on_progress, on_load_finished) {
        // cc.log("========res_mgr preload_res_pkg============", res_set);
        this.res_set = res_set;

        this.now_loaded = 0;
        this.total_num = 0;

        var key = null;
        for (key in res_set) {
            this.total_num += res_set[key].length;
        }

        if (this.total_num === 0) {
            if (on_load_finished) {
                on_load_finished();
            }
            return;
        }
        var self = this;
        for (const key in res_set) {
            for (let i = 0; i < res_set[key].length; i++) {
                var url = res_set[key][i];
                cc.loader.loadRes(url, function (err, obj) {
                    if (err) {
                        cc.error("[res_mg]:", err);
                    }
                    self.now_loaded++;

                    if (on_progress) {
                        // cc.log("========preload_res_pkg on_progress============")
                        on_progress(self.now_loaded / self.total_num);
                    }

                    if (self.now_loaded >= self.total_num) {
                        if (on_load_finished) {
                            // cc.log("========preload_res_pkg on_load_finished============")
                            on_load_finished();
                        }
                    }
                })
            }
        }
    },

    /**
     * 释放一个资源集合
     * @param {*} res_set 
     */
    release_res_pkg(res_set) {
        cc.log("===========res_mgr release_res_pkg===========")
        //释放sprite_frames
        if (res_set.sprite_frames && res_set.sprite_frames.length > 0) {
            cc.loader.release(res_set.sprite_frames)
        }
        //释放audio_clips
        if (res_set.audio_clips && res_set.audio_clips.length > 0) {
            cc.loader.release(res_set.audio_clips)
        }
        //释放sprite_atlases
        if (res_set.sprite_atlases && res_set.sprite_atlases.length > 0) {
            cc.loader.release(res_set.sprite_atlases)
        }

        if (res_set.prefabs && res_set.prefabs.length > 0) {
            for (let i = 0; i < res_set.prefabs.length; i++) {
                var url = res_set.prefabs[i];
                var deps = cc.loader.getDependsRecursively(url); //获取依赖的资源
                cc.log("===========res_mgr release_res_pkg=deps==========", deps)
                cc.loader.release(deps); // 释放预支依赖的资源
                cc.loader.release(url); //释放预制体
            }
        }
    },

    /**
     * 返回我们获取的资源
     * @param {*} url 
     */
    get_res(url) {
        return cc.loader.getRes(url);
    },

    start() {


    },

    update(dt) { },
});
